
var UnitTest = {
	
	assertDefined : function(obj, msg) {
		if (obj == undefined) {
			throw new Error(msg);
		}
	},
	
	assertTrue : function(obj, msg) {
		
		if (obj != true) {
			throw new Error(msg);
		}
	},
	
	assertFalse : function(obj, msg) {
	
		if (obj != false) {
			throw new Error(msg);
		}
	},
	
	assertEqual : function(expected, given) {
		
		if (expected != given) {
			throw new Error("expected: "+expected + " got: "+given);
		}
	},
	
	init : function() {
		
		var table = document.createElement("table");
		table.setAttribute("border", "1");
		table.setAttribute("width", "100%");
		var tbody = document.createElement("tbody");
		table.appendChild(tbody);
		if (! Tests){
			window.Tests = {};
		}
		for (var testName in Tests) {
			if (false) {
				if (testName == "up" || testName == "down") {
					continue;
				}
			}
			var test = Tests[testName];
			var start = new Date().getTime();
			try {
				Norm.log(testName + ", started...")
				test();
				Norm.log(testName + ", done!");
				UnitTest.appendGoodRow(tbody, testName, "", (new Date().getTime()) - start);
			} catch(e) {
				Norm.log(e);
				Norm.log(testName + ", error :)");
				UnitTest.appendBadRow(tbody, testName, e.message, (new Date().getTime()) - start);
			}
		}
		document.body.appendChild(table);
	},
	
	appendGoodRow : function(tbody, desc, msg, time) {
		UnitTest.appendRow(tbody, true, desc, msg, time);
	},
	
	appendBadRow : function(tbody, desc, msg, time) {
		UnitTest.appendRow(tbody, false, desc, msg, time);
	},
	
	appendRow : function(tbody, good, descMsg, msg, time) {
		
		var row = document.createElement("tr");
		row.setAttribute("style","border:1px solid #C0C0C0;");
		var desc = document.createElement("td");
		var status = document.createElement("td");
		var timeTd = document.createElement("td");
		desc.innerHTML = descMsg;
		status.innerHTML = good ? "OK" : msg;
		row.style.backgroundColor= good ? "#80FF00" : "red";
		if (time || time == 0) {
			timeTd.innerHTML = time + " ms";
		}
		row.appendChild(desc);
		row.appendChild(status);
		row.appendChild(timeTd);
		tbody.appendChild(row);
	}
};

window.onload = UnitTest.init;
